var CONFIG = require("../../utils/config.js")
var that;
const app = getApp()


Page({
  data: {
  tag:"编辑推荐",
  name:"测试1",
  lei:-1,
  fenlei:[],
  booklist:[],
  listtmp:true,
  count:0,
  loadingTip: "上拉加载更多~",
  hasmore:true
  },
  //事件处理函数

  onLoad: function () {
    var that=this 
    var lei = that.data.lei
    var uid=wx.getStorageSync('openId')
    that.querylei()
    if(uid){
   
    that.querybook(lei)
    }else{
      app.login()

    }
  },
  querybook:function(lei){
    var that=this
    wx.showLoading({
      title: '加载中...',
    })
    var url=CONFIG.URL.querybook
    var openId=wx.getStorageSync('openId')
    var count=that.data.count
    if(lei==(-1)){
      var data={
      "wxuid":openId,
      "count":count
      }
      }else{
        var data={
          "wxuid": openId,
          "lei":lei,
          "count": count
      }
      }
      CONFIG.post_from(url,data,function(res){
        setTimeout(function () {
          wx.hideLoading();
        
        }, 200);
       if(res.statusCode==200){
         console.log("结果数据为")
         console.log(res)
         if (res.data.booklist.length > 0) {
         var booklist = that.data.booklist

         var books=res.data.booklist
         var prolist = res.data.processlist

         var prolength = res.data.processlist.length
         var booklength = books.length
        console.log("注意process数据是否有:"+prolength)
        
         for (var i = 0; i < booklength; i++) {
           for (var j = 0; j < prolength; j++) {
             if (books[i].id == prolist[j].bookid) {
               console.log("===")
               books[i].process = {};
        
               books[i].process = prolist[j].process
               
             }
           }
         }
        
   

       
        that.setData({
          booklist:booklist.concat(books)
        })
        }else{
          that.setData({
            loadingTip: "没有了哦~",
            hasmore:false
          })
        }
       }
      },function(err){
        setTimeout(function () {
          wx.hideLoading();

        }, 580);
        console.log(err)
      })
  },
  // queryUserBook: function (bookid) {
  //   var that = this
  //   var wxuid = wx.getStorageSync('openId')
  //   var url = CONFIG.URL.queryUserBook
  //   var data = {
  //     "wxuid": wxuid,
  //     "bookid": bookid
  //   };
  //   CONFIG.post_from(url, data, function (res) {
  //     console.log("===1=8=")
  //     console.log(res)
  //     var zhang= res.data.bookinfo.zhang
  //       console.log(zhang)
  //     if(zhang>1){
  //       zhang=zhang
  //     }else{
  //       zhang=1
  //     }
  //     var process=res.data.process[zhang]
   
  //     if(process>1){
  //       process = res.data.process[zhang]
  //     }else{
  //       process=0
  //     }
  //     console.log(process)
  //   }, function (err) {
  //     console.log(err)
  //   })


  // },

  querylei:function(){
    var that=this
   
    var url = CONFIG.URL.querylei
    wx.request({
      url:CONFIG.URL.querylei,
      method: "POST",
      success:function(res){
        console.log("====")
        console.log(res.data)
        that.setData({
          fenlei:res.data
        })
      },
      fail:function(err){
        console.log("====2")
        console.log(err)
      }
    })
  },
  showDetail: function (e) {
    var that = this
    var uid = wx.getStorageSync('openId')
    if (!uid) {
      app.login();
    }else{
    var bookid = e.currentTarget.dataset.id
    var booktitle=e.currentTarget.dataset.title
    app.globalData.booktitle=booktitle;
    wx.request({
      url: CONFIG.URL.queryUserBook,
      
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      method: 'POST',
      data: {
        'wxuid': uid,
        'bookid': bookid
      },
      success:function(res){
        console.log(res)
        if (res.data.bookinfo.zhang){
        var process=res.data.process
        var bookinfo=res.data.bookinfo
        var viewInfo = bookinfo.zhang
        var jindu=0
        if (process.length > 0 && bookinfo.zhang > 0){
     
        for(var i=0;i<process.length;i++){
          if (process[i].zhang == bookinfo.zhang ){
            viewInfo=bookinfo.zhang
            jindu=process[i].zhang_process
            console.log("细节=1=")
            console.log(viewInfo)
            if(jindu>100){
              jindu=99
            console.log(jindu)
            }
          }
        }
       
        }else{
          viewInfo=1
          jindu=0
          console.log("细节3==")
          console.log(viewInfo)
          console.log(jindu)
        }
        console.log("最终数据==")
        console.log(viewInfo)
        console.log(jindu)
     
        app.showDetail(bookid,viewInfo,jindu);
        }else{
         var viewInfo = 1
          var jindu = 0
          console.log("未读数据 细节4==")
          console.log(viewInfo)
          console.log(jindu)
          app.showDetail(bookid, viewInfo, jindu);
        }
      },
      fail:function(err){
        console.log(err)
      }
    })
   
 
    // app.showDetail(bookid,viewInfo,process);
    }
  },
  
    changeId:function(e){
      var that=this
      var lei = e.currentTarget.dataset.id
      console.log(lei)
      that.setData({
        lei:lei,
        booklist:[],
        count:0,
        hasmore:true
      
      })
      that.querybook(lei)
      
    },
    changeStyle:function(e){
      var that=this
      var listtmp = that.data.listtmp
      if (listtmp==true){
        that.setData({
          listtmp:false
        })
      }else{
        that.setData({
          listtmp:true
        })
      }
    },
    onReachBottom: function () {
      var that = this
      var openId = wx.getStorageSync('openId')
      var lei=that.data.lei
      if (!that.data.hasmore) {
        that.setData({
          loadingTip: "没有了哦~"
        });
        return;
      } else {
        var count = that.data.count
        that.setData({
          count: count+20
        });
        var uid = wx.getStorageSync('openId')
          that.querybook(lei)
      }
    },
  onShow:function(){
    var that = this
    var lei = that.data.lei
    var uid = wx.getStorageSync('openId')
    that.querylei()
    if (uid) {
      
      that.querybook(lei)
    } else {
      app.login()

    }
  },
  onHide:function(e){
    console.log(e)
    console.log("测试中")
    var that=this
    that.setData({
      lei: -1,
      booklist: [],
      listtmp: true,
      count: 0,
      hasmore: true
    })
  }
})
